#!/bin/bash

#
# 构建web server环境
#
#

source "/root/common"

config_path="/root/.script.json"
eval set -- "$(getopt -q -o hvc: -l config: -- "$@")"

while [ -n "$1" ]; do
  case "$1" in
  -h | --help)
    echo ""
    echo "Install common script files in the Linux operating system"
    echo ""
    echo "Usage:"
    usageFormat "-h,--help" "get help"
    usageFormat "-v,--version" "show version"
    usageFormat "-c,--config" "set config file,default /root/.config.json"
    exit
    ;;
  -v | --version)
    echo "version: v1.0.0"
    exit
    ;;
  -c | --config)
    config_path=$2
    shift 2
    ;;
  --)
    shift 1
    break
    ;;
  *)
    break
    ;;
  esac
done

declare -A local_storage
local_storage['conf']=''
local_storage['compose']=''
local_storage['dockerfile']=''
local_storage['script']=''
for item in "${!local_storage[@]}"; do
  dir=$(jq <"$config_path" -r ".local.storage.$item")
  if [ "${#dir}" -eq 0 ]; then
    printError "The $item parameter in the $config_path file is null"
  fi
  local_storage[$item]=$(jq <"$config_path" -r ".local.storage.$item")
done

echo ""
echo "Create local storage directory"
for item in "${local_storage[@]}"; do
  if [ ! -d "${item}" ]; then
    mkdir -p "${item}" >/dev/null 2>&1
  fi

  if [ ! -d "${item}" ]; then
    printf "%+2s%+30s%+10s\n" "" "${item} " "directory create failed"
  else
    printf "%+2s%+30s%+10s\n" "" "${item} " "directory create completed"
  fi
done

ok() {

  #
  # 下载dockerfile
  # 下载php mysql nginx基础的dockerfile文件用于构建lnmp服务环境
  #
  command="${local_storage['script']}/download-dockerfile"

  if [ -f "$command" ]; then
    "$command" -y --server "nginx" --version "1.22.1"
    "$command" -y --server "php" --version "fpm-7.4.33/product"
    "$command" -y --server "mysql" --version "5.7.40"
  fi

  # 安装docker
  if ! which docker >/dev/null 2>&1; then
    script="${local_storage['script']}/docker-install"
    if [ -f "$script" ]; then
      echo ""
      echo "Prepare to execute docker-install script"
      echo ""
      "$script"
    fi
  fi

  #
  # 创建挂载目录和文件
  #
  # php、mysql、nginx
  #
  script="${local_storage['script']}/download-conf"
  script="/root/test/scripts/download/download-conf"
  if [ -f "$script" ]; then
    echo "Docker volume:"
    "$script" -y --server "nginx" --version "1.22.1"
    "$script" -y --server "php" --version "7.4.33"
    "$script" -y --server "mysql" --version "5.7.40"
  fi

  # 启动docker后才能build
  echo ""
  echo "Docker Start: "
  tries=0
  while true; do
    if [ $tries -eq 5 ]; then
      echo "  start fail,Please start docker"
      exit 1
    fi
    status=$(sudo ps -aux | grep docker | grep -v "grep")
    if [ -z "$status" ]; then
      sudo systemctl start docker >/dev/null 2>&1
    else
      echo "  start completed"
      break
    fi
    tries=$(("$tries" + 1))
  done

  compose="${local_storage['script']}/docker-make-compose"
  if [ -f "$compose" ]; then
    echo ""
    echo "Make docker compose.yml file，include nginx php"
    sudo "$compose" -o "${local_storage['compose']}" nginx php mysql >/dev/null 2>&1
    if [ -f "${local_storage['compose']}/docker-compose.yml" ]; then
      echo "docker-compose.yml create completed,in ${local_storage['compose']}"
    fi
  fi

  html="${local_storage['compose']}/html"
  if [ ! -d "$html" ]; then
    sudo mkdir "$html" >/dev/null 2>&1
  fi

  index="$html/index.html"
  if [ ! -f "$index" ]; then
    sudo touch "$index" >/dev/null 2>&1
    sudo chmod 777 "$index"
    sudo echo "<h1>hello world</h1>" >"$index"
  fi

  index="$html/index.php"
  if [ ! -f "$index" ]; then
    sudo touch "$index" >/dev/null 2>&1
    sudo chmod 777 "$index"
    sudo echo -e "<?php \n phpinfo();\n" >"$index"
  fi
}

ok
